# -*- coding: utf-8 -*-
import requests
import re
import json

def get_vid(url):
    response = requests.get(url)
    html = response.text
    href = re.search(r'<link rel="canonical" href="(.*?)"',html).group(1)
    ht = href.split('/')[-1]
    vid = re.search(r'(.*?).html', ht).group(1)
    return vid

def get_info_url(vid):

    # 默认的参数
    json_type='json'
    platform = '11001'
    guid='ba7f7fab8f8aef2c4ae45883f221c04d'
    sdtfrom = 'v1010'
    _qv_rmt='703hXQMOA1937593I%3D'
    _qv_rmt2='egp3C2aj149060UUA%3D'
    # getinfo的base_url
    base_info_url = 'https://h5vv.video.qq.com/getinfo?vid={vid}&otype={otype}&guid={guid}&platform=\
    {platform}&sdtfrom={sdtfrom}&_qv_rmt={_qv_rmt}&_qv_rmt2={_qv_rmt2}'
    # format方法生成getinfo
    info_url = base_info_url.format(vid=vid, otype=json_type, guid=guid, platform=platform, \
                                    sdtfrom=sdtfrom, _qv_rmt=_qv_rmt, _qv_rmt2=_qv_rmt2)
    return info_url

def get_keys(info_url):
    response = requests.get(info_url)
    html = response.text
    # 正则表达式提取json数据并序列化为json格式
    data = re.search('=(.*);', html).group(1)
    data = json.loads(data)
    for each in data.get('vl').get('vi'):
        fvkey = each.get('fvkey')
        fn = each.get('fn')
        self_host = each.get('ul').get('ui')[0].get('url')

        keys={
            'fvkey':fvkey,
            'fn':fn,
            'self_host':self_host
        }
        return keys

def get_real_url(keys):
    # 真实地址的base_url
    base_real_url = '{self_host}{fn}?vkey={fvkey}'
    # 组装视频真实地址
    real_url = base_real_url.format(self_host=keys['self_host'],fn=keys['fn'], fvkey=keys['fvkey'])
    return real_url


def main():
    # url = input('请输入视频链接:')
    url = 'https://v.qq.com/x/page/c0334eajum1.html'
    vid = get_vid(url)
    print "vid="+ vid
    info_url = get_info_url(vid)
    keys = get_keys(info_url)
    real_url = get_real_url(keys)

    print('视频真实地址为:', real_url)


if __name__ == '__main__':
    main()
